package com.vertx.web.explain;

import com.vertx.core.annotation.base.ClassAnnotationExplain;
import com.vertx.core.beam.AbstractBeanFactory;
import com.vertx.core.beam.BeanContainer;
import com.vertx.core.exception.AnnotationExplainUnMatchedException;
import com.vertx.web.annotation.Rest;
import org.apache.log4j.Logger;

import java.lang.annotation.Annotation;

/**
 * 描述:被标识的类自动注入
 * 创建人:hzw
 * 创建时间: 2018/4/18 15:17
 **/
public class RestExplain implements ClassAnnotationExplain {

    private static final Logger logger = Logger.getLogger(RestExplain.class);

    @Override
    public void handler(Class classes, Annotation annotation, AbstractBeanFactory beanFactory, Object[] params) throws Exception {
        String name;
        if (annotation instanceof Rest) {
            name = ((Rest) annotation).name();
        } else {
            logger.error("注解解释器不匹配");
            throw new AnnotationExplainUnMatchedException("注解解释器不匹配");
        }
        BeanContainer beanContainer = new BeanContainer(classes);

        if (name.length() > 0) {
            //如果注册bean的时候指定了名称，就使用指定的名称
            beanContainer.setBeanName(name);
        } else {
            beanContainer.setBeanName(beanFactory.getSimpleClassName(classes));
        }
        beanFactory.registerBean(beanContainer.getBeanName(), beanContainer);
        logger.debug("register: " + beanContainer.getBeanName());
    }

    @Override
    public Annotation getExplainClass() {
        return ()->Rest.class;
    }
}
